<?php

/**
 * ApplicationCorporateManaged form.
 *
 * @package    acca
 * @subpackage form
 * @author     Rommel M. Zamora / Sonny L. Cabali
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ApplicationCorporateManagedForm extends BaseApplicationCorporateManagedForm
{

	protected $assignees;
	protected $assigneesForDeletion = array();

  public function configure()
  {
    unset($this['created_at'], $this['updated_at'], $this['ref_number']);

    $this->embedRelation('Assignee AS assignees');

    // set years to minimum of 18 years old
    $years = range(date('Y')-18, 1960);

    $this->widgetSchema['business_address']->setLabel('Room/Flr/Bldg Name/No./Street');
    $this->widgetSchema['business_brgy_municipality_town']->setLabel('Brgy/Municipality/Town');
    $this->widgetSchema['business_city_province']->setLabel('City/Province');
    $this->widgetSchema['business_postal_code_zip_code']->setLabel('Postal/Zip code');
    $this->widgetSchema['authorized_corporate_officer_signatory1']->setLabel('Authorized corporate/officer signatory');
    $this->widgetSchema['authorized_corporate_officer_signatory1_position']->setLabel('Position in company');
    $this->widgetSchema['authorized_corporate_officer_signatory1_contact_number']->setLabel('Contact No.');
    $this->widgetSchema['authorized_corporate_officer_signatory1_email_address']->setLabel('E-mail address');
    $this->widgetSchema['authorized_corporate_officer_signatory2']->setLabel('Authorized corporate/officer signatory');
    $this->widgetSchema['authorized_corporate_officer_signatory2_position']->setLabel('Position in company');
    $this->widgetSchema['authorized_corporate_officer_signatory2_contact_number']->setLabel('Contact No.');
    $this->widgetSchema['authorized_corporate_officer_signatory2_email_address']->setLabel('E-mail address');
    $this->widgetSchema['after_sales_signatory_ies1']->setLabel('After-sales signatory/IES');
    $this->widgetSchema['after_sales_signatory_ies1_position']->setLabel('Position in company');
    $this->widgetSchema['after_sales_signatory_ies1_contact_number']->setLabel('Contact No.');
    $this->widgetSchema['after_sales_signatory_ies1_email_address']->setLabel('E-mail address');
    $this->widgetSchema['authorized_corporate_officer_signatory3']->setLabel('Authorized corporate/officer signatory');
    $this->widgetSchema['authorized_corporate_officer_signatory3_position']->setLabel('Position in company');
    $this->widgetSchema['authorized_corporate_officer_signatory3_contact_number']->setLabel('Contact No.');
    $this->widgetSchema['authorized_corporate_officer_signatory3_email_address']->setLabel('E-mail address');
//    $this->widgetSchema['ref_number']->setDefault(RandomNameGenerator::transactionCode());
//    $this->widgetSchema['ref_number']->setAttribute('readonly','readonly');

    // Add email validation to email fields
    $emailFields = array(
      'authorized_corporate_officer_signatory1_email_address',
      'authorized_corporate_officer_signatory2_email_address',
      'after_sales_signatory_ies1_email_address',
      'authorized_corporate_officer_signatory3_email_address'
    );
    foreach($emailFields as $emailField) {
      $this->validatorSchema[$emailField] = new sfValidatorEmail($this->validatorSchema[$emailField]->getOptions());
    }

    $numbersOnlyFields = array(
        'business_postal_code_zip_code',
        'authorized_corporate_officer_signatory1_contact_number',
        'authorized_corporate_officer_signatory2_contact_number',
        'after_sales_signatory_ies1_contact_number',
        'authorized_corporate_officer_signatory3_contact_number',
        'bill_summary_contact_number'
    );

    $lettersOnlyFields = array(
        'company_name',
        'authorized_corporate_officer_signatory1',
        'authorized_corporate_officer_signatory2',
        'after_sales_signatory_ies1',
        'authorized_corporate_officer_signatory3',
        'bill_summary_contact_name'
    );

    $validatorSchema = $this->validatorSchema;
    foreach($validatorSchema->getFields() as $key=>$field) {
      $validators = array();

      if($field->getOption('required')) {
        $validators[] = 'required';
        $label = ($this->widgetSchema[$key]->getLabel()) ? $this->widgetSchema[$key]->getLabel() : $this->widgetSchema->getFormFormatter()->generateLabelName($key);
        $this->widgetSchema[$key]->setLabel($label.' *');
      }
      else {
        $validators[] = 'optional';
      }

      // add max_length based on length in schema
      if($field->getOption('max_length')) {
        $validators[] = 'maxSize['.$field->getOption('max_length').']';
      }

      // if integer, add class integer
      if(get_class($field) == 'sfValidatorInteger') {
        $validators[] = 'custom[integer]';
      }

      // if email, add class email
      if(get_class($field) == 'sfValidatorEmail') {
        $validators[] = 'custom[email]';
      }

      // if numbers only, add class onlyNumberSp
      if(in_array($key,$numbersOnlyFields)) {
        $validators[] = 'custom[onlyNumberSp]';
      }

      // if letters only, add class onlyNumberSp
      if(in_array($key,$lettersOnlyFields)) {
        $validators[] = 'custom[onlyLetterSp]';
      }

      $this->widgetSchema[$key]->setAttribute('class','validate['.implode(',', $validators).']');
    }


    // add 'dependent' class to dependent fields (used for hiding/showing them in view)
    $dependentFields = array(
        'vat_bir_certificate_account_number'=>'vat',
        'oct_bir_certificate_account_number'=>'oct',
        'bill_soft_copy_to_email_address'=>'receive_bill_summary',
        'bill_hard_copy_to_business_address'=>'receive_bill_summary',
        'bill_summary_contact_name'=>'receive_bill_summary',
        'bill_summary_contact_number'=>'receive_bill_summary',
        'bill_summary_type'=>'receive_bill_summary',
        'bill_recipient_company_name'=>'corporate_managed_bills_sent_to',
        'bill_recipient_attention'=>'corporate_managed_bills_sent_to',
        'bill_recipient_unit_number_floor'=>'corporate_managed_bills_sent_to',
        'bill_recipient_department'=>'corporate_managed_bills_sent_to',
        'bill_recipient_building_street'=>'corporate_managed_bills_sent_to',
        'breakdown_department1'=>'bill_summary_type',
        'breakdown_department1_contact_name_number'=>'bill_summary_type',
        'breakdown_department1_bill_cut_off'=>'bill_summary_type',
        'breakdown_department1_corporate_id'=>'bill_summary_type',
        'breakdown_department2'=>'bill_summary_type',
        'breakdown_department2_contact_name_number'=>'bill_summary_type',
        'breakdown_department2_bill_cut_off'=>'bill_summary_type',
        'breakdown_department2_corporate_id'=>'bill_summary_type',
        'breakdown_department3'=>'bill_summary_type',
        'breakdown_department3_contact_name_number'=>'bill_summary_type',
        'breakdown_department3_bill_cut_off'=>'bill_summary_type',
        'breakdown_department3_corporate_id'=>'bill_summary_type',
        'breakdown_department4'=>'bill_summary_type',
        'breakdown_department4_contact_name_number'=>'bill_summary_type',
        'breakdown_department4_bill_cut_off'=>'bill_summary_type',
        'breakdown_department4_corporate_id'=>'bill_summary_type',
        'breakdown_department5'=>'bill_summary_type',
        'breakdown_department5_contact_name_number'=>'bill_summary_type',
        'breakdown_department5_bill_cut_off'=>'bill_summary_type',
        'breakdown_department5_corporate_id'=>'bill_summary_type'
        );
    foreach($dependentFields as $key=>$value) {
      $this->widgetSchema[$key]->setAttribute('class',$this->widgetSchema[$key]->getAttribute('class').' dependent');
      $this->widgetSchema[$key]->setAttribute('data-dependent','application_corporate_managed_'.$value);
    }



  }

	/* Added by rommel m. zamora
	 * Form Embedding the right way going around errors of symfony embed form
	 */

	public function setAssignees($assignees) {
		$this->assignees = $assignees;
	}

	public function getAsignees() {
		return $this->assignees;
	}

  public function getAssigneesForDeletion() {
      return $this->assigneesForDeletion;
  }

  public function addAssignees($num, $container='assignees'){
    $assignee = new Assignee();
		// assign the parent object
    $assignee->setApplicationCorporateManaged($this->getObject());
		// create a new form
    $assignee_form = new AssigneeForm($assignee);
    //Embedding the new  Assignee in the container
    $this->embeddedForms[$container]->embedForm($num, $assignee_form);
    //HACK Re-embedding the container
    $this->embedForm($container, $this->embeddedForms[$container]);
  }

	/*
	 * override symfony bind function
	 */
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    //embeded form assignee
    if (isset ($taintedValues['assignees'])) {
      foreach ($taintedValues['assignees'] as $key => $values) {

      		// check required fields
          if ('' === trim($taintedValues['assignees'][$key]['name']) &&
          	  '' === trim($taintedValues['assignees'][$key]['subscription'])) {
          	  // don't save empty form
              unset($taintedValues['assignees'][$key]);
					// add filled out forms
          } else {
              if (isset ($taintedValues['assignees'][$key]['delete'])) {
                  $this->sizeForDeletion[$key] = $taintedValues['sizes'][$key]['id'];
              } else {
                  if (!isset($this['assignees'][$key])) {
                      $this->addAssignees($key, 'assignees');
                  }
              }
          }

          // check if Delete checkbox is checked
          if(isset($values['delete'])) {
            if($values['delete'] == 'Yes') {
              // add to Assignees for Deletion
              $this->assigneesForDeletion[] = $values['id'];
            }
          }


      }
    }
    parent::bind($taintedValues, $taintedFiles);
  }


}
